var router = require('libs/router').router;
var pages = require('libs/pages');
var ItemMixin = require('mixins/item');
var libs = require('libs/libs');
var api = libs.api;
var valide = libs.formValide;
var Radio = require('modules/form/radio1')(true)
var List = require('modules/list/load_list')(true, 'Address_list');
var _user;


//当前页公共变量
var _page = {}
var _page_instance;

var _addresslist_li = [];

function radioClick(){
    $(this).click(function(){
        var _id = $(this).find('input[type=radio]').val();
        var postdata = {
            login: _user.login,
            method: 'get',
            id: _id
        }
        api.req('/setAddressToDefault', postdata, function(data){
            // router.goback()
        })
    })
}

function bindShow(){
    $('.addresslist_addadd_submit').click(function(){
        // router('uc/edit')
        router('uc/addressedit',{count: _addresslist_li.length})

    })
}

var bindEvent = function(){
    var me = this;
    $(this).find('.addresslist_del').on('click', function(){
        var _this = this;

        ui.alertBox({
            cons: [{text: '确定删除地址', name: "bigCon"}],
            btns: [
                {
                    text: '取消',
                    close: true
                },
                {
                    text: '确认',
                    close: true,
                    callback: function(){
                        _delAccess()
                    }
                }
            ]
        })
        function _delAccess(){
            var postdata = {
                login: _user.login,
                method: 'get'
            }
            var _id = $(_this).attr('data-id')
            postdata.id = _id;
            api.req('/deleteUserAddress', postdata, function(data){
                if (data && !data.code){
                    libs.msgtips('删除一条地址')
                    getAddList()
                }
            })
        }
    })

    $(this).find('.addresslist_edit').on('click', function(){
        var postdata = {
            login: _user.login,
            method: 'get'
        }
        var _id = $(this).attr('data-id')
        postdata.addressId = _id;

        // 单条地址数据
        // {
        //     "address": "就不告诉你，我在哪里111",
        //     "communityId": "1",
        //     "communityName": "",
        //     "contact": "yaocheng111",
        //     "contactPhone": "13268280401",
        //     "createTime": "2016-04-28 11:52:49",
        //     "dscd": "440111",
        //     "fullAddress": "广东省广州市白云区君华香柏广场就不告诉你，我在哪里111",
        //     "id": "698bf70536634863af0e1220131cdb6b",
        //     "isCommunityuser": true,
        //     "isDefaultAdd": false,
        //     "isDeleted": false,
        //     "postcode": "",
        //     "street": "",
        //     "updateTime": null,
        //     "userId": "c37142306da340d4acc311d1730a9d9c"
        // }
        api.req('/getUserAddressById', postdata, function(data){
            if (data && !data.code){
                var uuinfo = {
                    address_edit_name: data.contact,
                    address_edit_mobile: data.contactPhone,
                    address_edit_address: data.address,
                    address_edit_zone: (function(){return data.fullAddress.replace(data.address,''); })(),
                    addressId: _id,
                    isDefaultAdd: data.isDefaultAdd,
                    back: 'uc/addresslist'
                }
                router('uc/addressedit', uuinfo)
            }
        })
    })
};


//演示模块
var Show = React.createClass({
    mixins: [ItemMixin],
    render:function(){
        if(this.props.data){
            address_list_data = this.props.data;
        }
        return (
            <div className="app_addresslist flex flex-v">
                <div className='flex-1 f-oTouch'>
                    <List data={address_list_data} itemMethod={bindEvent} listClass='address_list' itemClass="address_list_item"/>
                </div>
                <div className="default_foot" style={{padding:"0.1rem 0"}}>
                    <div className="f_submit" style={{width:'100%'}}>
                        <input type="submit" defaultValue="添加地址" className="f_btn addresslist_addadd_submit" />
                    </div>
                </div>
            </div>
        );
    }
});

function getAddList(){
    var self = this;

    _addresslist_li=[]

    var addresslist_post = {
        login: _user.login,
        method: 'get'
    }
    console.log(addresslist_post);
    api.req('/getListUserAddress', addresslist_post, function(data){
        console.log(data);
        if(data && data.length){
            var _value='novalue';
            data.map(function(item, i){
                _value = item.id
                if (item.isDefaultAdd){
                    _value = '-'+_value
                }
                _addresslist_li.push(
                    <div className="addresslist_item">
                        <em>{item.contact}</em>
                        <p>{item.fullAddress}</p>
                        <div className="bar">
                            <div className="part part-left">
                                <Radio name="default_address" value={_value} title="默认地址" itemMethod={radioClick} />
                            </div>
                            <div className="part part-right">
                                <span data-id={item.id} className="btn addresslist_edit">编辑</span>
                                <span data-id={item.id} className="btn addresslist_del">删除</span>
                            </div>
                        </div>
                    </div>
                )
            })
            _addresslist_li.push(<div style={{height:"0.8rem",backgroundColor:"#f7f7f7",margin:'-0.2rem -0.24rem;'}}></div>)

            SA.setter('Address_list',{data: _addresslist_li})
            self.main()
        }
        else {
            SA.setter('Address_list',{data: []})
            _page_instance.main()
            // setTimeout(function(){
            //     router('uc/addressedit')
            // },300)
        }
    })
}

function start(name){
    _page_instance = pages.new({
        boot:function(){
            // getAddList()
        },
        trigger:function(){
            sessionStorage.removeItem('_tmp_new_address')             
            SA.deleter('Uuinfo');
            this.libs.changeTitle('地址维护');    //更改当前页面标题
            _user = SA.get('USER')
            getAddList.call(this)
        },
        goback: function(){
            var gnext = SA.get('_GoBack')
            if (gnext){
                router.cb = function(){
                    SA.deleter('_GoBack')
                    router(gnext)
                }
            }
            else{
                var _pre = router.pre();
                if (_pre && _pre.hash==='uc/addressedit'){
                    router.cb = function(){
                        router('uc/edituser')
                    }
                }
                else {
                    router.cb = function(){
                        router.goback()
                    }
                }
            }
        },
        ready: function(){
        },
        main: function(){
            this.render(
                <Show  data={_addresslist_li} itemMethod={bindShow}/>,
                document.getElementById(name)
            )
        },
        end: function(){
            _addresslist_li= []
            React.unmountComponentAtNode(document.getElementById(name))
        }
    })
    return _page_instance;
}

module.exports = start;
